﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Net;
using System.Net.Mail;

namespace EmailService
{
    /// <summary>
    /// Summary description for EmailService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class EmailService : System.Web.Services.WebService
    {

        [WebMethod]
        public bool sendEmail(String to, String subject, String body)
        {
            bool success = true;
            // create mail message object
            MailMessage mail = new MailMessage();
            mail.From = new MailAddress("bpilibrarysystem@gmail.com");           // put the from address here
            mail.To.Add(to);             // put to address here
            mail.Subject = subject;        // put subject here	
            mail.Body = body;           // put body of email here

            mail.IsBodyHtml = true;
            mail.Priority = MailPriority.Normal;

            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com", 587); //use this PORT!
            SmtpServer.EnableSsl = true;
            SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
            SmtpServer.Credentials = new NetworkCredential("bpilibrarysystem@gmail.com", "Password1234");
            // and then send the mail
            try
            {
                SmtpServer.Send(mail);
            }
            catch (Exception ex)
            {
                success = false;
            }
            return success;
        }
    }
}
